import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  ParseIntPipe,
  Logger,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { UpdateUserDto } from './dto/update-user.dto';
import { CreateUserDto } from './dto/create-user.dto';

import { AuthGuard } from '../auth/auth.guard';
import { User } from './entities/user.entity';
import { ConfigService } from '@nestjs/config';
import { ConfigEnum } from 'src/enum/config.enum';

@Controller('users')
export class UsersController {
  private logger = new Logger(UsersController.name);
  constructor(
    private readonly usersService: UsersService,
    private configService: ConfigService,
  ) {
    this.logger.log('UserController init');
  }

  @Post()
  @UseGuards(AuthGuard)
  async createUser(@Body() user: User): Promise<User> {
    return this.usersService.createUser(user);
  }

  @Get()
  async findAll(): Promise<User[]> {
    const db = this.configService.get('DB_DATABASE');
    console.log('~ file ----', db);
    this.logger.log('UserController init');
    const password =
      process.env.DB_PASSWORD || this.configService.get(ConfigEnum.DB_PASSWORD);
    console.log('~ process env ----', password);
    return this.usersService.findAllUsers();
  }

  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.usersService.findOne(id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.usersService.remove(+id);
  }

  @Get('/profile/:id')
  getUserProfile(@Param('id') id: number): any {
    return this.usersService.findProfile(id);
  }

  @Get('/logs/:id')
  getUserLogs(@Param('id') id: number): any {
    return this.usersService.findUserLogs(id);
  }

  @Get('/logsByGroup/:id')
  async getLogsByGroup(@Param('id') id: number): Promise<any> {
    const res = await this.usersService.findLogsByGroup(id);
    return res.map((o) => ({
      result: o.result,
      count: o.count,
    }));
  }
}
